package com.easemob.helpdesk.widget;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;

import com.housekeeperapp.R;

/**
 * Created by xiaolong.wei on 2019/6/11.
 */

public class SwitcherButton extends RelativeLayout implements View.OnClickListener{
    private static final String TAG = "SwitcherButton";

    View view;
    RelativeLayout background;
    View circle;
    int state;//0为关；1为开
    private int distance;
    private String origin;

    OnChangeStateListener onChangeStateListener;

    Animator animator;
    private long duration;

    public SwitcherButton(Context context) {
        this(context,null);
    }

    public SwitcherButton(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public SwitcherButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        view = LayoutInflater.from(context).inflate(R.layout.switcher_button,this);
        background = view.findViewById(R.id.background);
        circle = view.findViewById(R.id.circle);


        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.switcher);
        state = typedArray.getInt(R.styleable.switcher_state,1);
        duration = 400;
        initView();


    }

    private void initView(){
        RelativeLayout.LayoutParams layoutParams = ((RelativeLayout.LayoutParams) circle.getLayoutParams());
        layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
        if(state == 1){
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            origin = "right";
            circle.setBackgroundResource(R.drawable.switcher_circle_button_open);
            background.setBackgroundResource(R.drawable.switcher_button_open_bg);
        }else {
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            origin = "left";
            circle.setBackgroundResource(R.drawable.switcher_circle_button_close);
            background.setBackgroundResource(R.drawable.switcher_button_close_bg);
        }
        circle.setLayoutParams(layoutParams);
    }



    @Override
    public void onClick(View v) {
        if (onChangeStateListener == null){
            return;
        }
        onChangeStateListener.beforeChange(state);
    }

    public void updateState(){
        distance = background.getWidth() - circle.getWidth()-background.getPaddingLeft()-background.getPaddingRight();
        Log.d(TAG, "onClick: distance"+distance);
        if (state == 0){//当前关闭
            state = 1;
            if (origin == "right") {
                startAnimation(-distance,0f,R.drawable.switcher_button_open_bg,R.drawable.switcher_circle_button_open);
            }else {
                startAnimation(0f,distance,R.drawable.switcher_button_open_bg,R.drawable.switcher_circle_button_open);
            }

        }else {//当前开启
            state = 0;
            if (origin == "right") {
                startAnimation(0f,-distance,R.drawable.switcher_button_close_bg,R.drawable.switcher_circle_button_close);
            }else{
                startAnimation(distance,0f,R.drawable.switcher_button_close_bg,R.drawable.switcher_circle_button_close);
            }
        }
    }

    private void startAnimation(final float start , float end, final int backgroundResId, final int circleResId){
        ObjectAnimator translationX = ObjectAnimator.ofFloat(circle, "translationX", start, end).setDuration(duration);
        translationX.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                background.setBackgroundResource(backgroundResId);
                circle.setBackgroundResource(circleResId);
                onChangeStateListener.onStateChanged(state);
                duration = 400;
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        translationX.start();

    }

    public void setState(int state){
        this.state = state;
        initView();
    }

    public void setOnChangeStateListener(OnChangeStateListener onChangeStateListener){
        this.onChangeStateListener = onChangeStateListener;
        background.setOnClickListener(this);
    }

    public interface OnChangeStateListener{
        void beforeChange(int state);
        void onStateChanged(int state);
    }
}
